$(function () {
    $("#search").css("display","block");
    $("#to_append").css("display","block");
    $("#cancel").css("display","none");
    $("#save").css("display","none");

    $("#seq_input").css("display","none");
})

$("#btn_to_append").click(function () {
    $("#search").css("display","none");
    $("#to_append").css("display","none");
    $("#cancel").css("display","block");
    $("#save").css("display","block");

    $("#className").val("");
    $("#firstId").val(0);
    $("#secondId").val(0);
})

$("#btn_cancel").click(function () {
    $("#search").css("display","block");
    $("#to_append").css("display","block");
    $("#cancel").css("display","none");
    $("#save").css("display","none");

    $("#seq_input").css("display","none");

    $("#className").val("");
    $("#firstId").val(0);
    $("#secondId").val(0);

    $("#thridId").val(0);
})

$("#btn_save").click(function () {

    var model_path=$("#form1").attr("model_path");
    var submit_url="";

    if( $("#thridId").val()==0){
        submit_url=model_path+"/append";
    }else{
        submit_url=model_path+"/modify";
    }

    $("#form1").prop("action",submit_url).submit();
})

function modifyThridClass(element) {

    $("#search").css("display","none");
    $("#to_append").css("display","none");
    $("#cancel").css("display","block");
    $("#save").css("display","block");

    $("#seq_input").css("display","block");

    var model_path=$("#form1").attr("model_path");
    var id=$(element).attr("id");
    var submit_url=model_path+"/detail?id="+id;

    $.get(submit_url,function (thrid) {
        $("#className").val(thrid.className)
        $("#firstId").val(thrid.firstId);
        $("#seq").val(thrid.seq);
        $("#thridId").val(thrid.id);

        firstChange ($("#firstId"),thrid.secondId);
    });
}

function firstChange (element,selectedId) {
    var firstId=$(element).val();
    var model_path=$("#form1").attr("model_path");
    var submit_url=model_path+"/listByFirstId?firstId="+firstId;

    $.get(submit_url,function (secondClasses) {
        var content='<option value="0">请选择所属二级分类</option>';
        $(secondClasses).each(function (index,item) {
            if(selectedId==item.id)
                content+='<option value="'+item.id+'" selected="selected">'+item.className+'</option>';
            else
                content+='<option value="'+item.id+'">'+item.className+'</option>';
        })
        $("#secondId").empty().html(content);
    });
}